package com.pro.controller;

import com.pro.pojo.JsonData;
import com.pro.pojo.LoginRecord;
import com.pro.service.LoginRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/loginRecord")
public class LoginRecordController {

    @Autowired
    private LoginRecordService loginRecordService;

    @RequestMapping("/getLoginRecordAll")
    @ResponseBody
    public JsonData getLoginRecordAll(){
        List<LoginRecord> loginRecordAll = loginRecordService.getLoginRecordAll();
        Collections.reverse(loginRecordAll);//倒序
        return loginRecordAll != null ? JsonData.buildSuccess(loginRecordAll):JsonData.buildError("服务器出错了");
    }

    //获取最后一次登录时间
    @RequestMapping(value = "/getLastLogin")
    @ResponseBody
    public JsonData getLoginRecord() throws ParseException {
        String lastTime = loginRecordService.getLastLoginRecord();
        Date parse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(lastTime);
        lastTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E").format(parse);
        return lastTime != null ? JsonData.buildSuccess(lastTime) : JsonData.buildError("服务器出错了");
    }
}
